package org.git.movie.service.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.git.movie.entity.FfList;
import org.git.movie.mapper.FfListMapper;
import org.git.movie.service.FfListService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import org.git.movie.vo.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 */
@Service
public class FfListServiceImpl   implements FfListService {

    @Resource
    private     FfListMapper ffListMapper;


    private  String allowKeys = "area,year,star";

    @Override
    public FfList findById(Integer id) {
        return ffListMapper.findById(id);
    }

    @Override
    public List<ListExtendVo> pareseEntend2ListExtendVo(String listExtend) {
        if(StringUtils.isEmpty(listExtend)){
            return null;
        }else{
            List<ListExtendVo> list = new ArrayList<>();
            JSONObject extendJson = JSON.parseObject(listExtend);
            Set<String> keySet = extendJson.keySet();
            String values = null;



            for(String key : keySet){
                if(allowKeys.indexOf(key) <= -1){
                    continue;
                }
                values = extendJson.getString(key);

                if(StringUtils.isNotEmpty(values)){
                    ListExtendVo keyListVo = new ListExtendVo();
                    keyListVo.setKey(key);
                    keyListVo.setShow(transKey2Name(key));

                    String[] valueArray = values.split(",");
                    for(String item :valueArray){
                        ListExtenValueVo valueVo = new ListExtenValueVo();
                        valueVo.setShow(item);

                        keyListVo.getValues().add(valueVo);
                    }

                    list.add(keyListVo);
                }
            }
            return list;
        }
    }

    private String transKey2Name(String key){
        if(key.equals("area")){
            return "地区";
        }
        if(key.equals("year")){
            return "年代";
        }
        if(key.equals("star")){
            return "演员";
        }
        throw new RuntimeException("不支持的类型");
    }


}
